package com.ldk.framework.service.impl;

import java.io.IOException;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ldk.base.framework.dao.IBaseDAO;
import com.ldk.base.framework.service.impl.BaseService;
import com.ldk.framework.service.IRoleMenuService;
import com.ldk.framework.service.IRoleService;
import com.ldk.framework.dao.IRoleDAO;
import com.ldk.framework.entity.Role;
import com.ldk.framework.entity.RoleMenu;

/**
* RoleService接口实现类
* @author xiangwl
*/
@Service
public class RoleService extends BaseService<Role> implements IRoleService {

	@Autowired
	private IRoleDAO roleDAO;
	@Autowired
	private IRoleMenuService roleMenuService;

	@Override
	protected IBaseDAO<Role> getBaseDao() {
     	return this.roleDAO;
	}
	
	@Override
	@Transactional(rollbackFor={Exception.class, NullPointerException.class, SQLException.class, IOException.class})
	public Boolean setRoleMenus(String dbKey, int roleId, int[] menuIds) throws Exception {
		if(roleId < 1 || menuIds == null || menuIds.length < 1) 
			return false;
		RoleMenu roleMenuQuery = new RoleMenu();
		roleMenuQuery.setRoleId(roleId);
		
		roleMenuService.delete(dbKey, roleMenuQuery);
		
		for (int menuId : menuIds) {
			RoleMenu entity = new RoleMenu();
			entity.setRoleId(roleId);
			entity.setMenuId(menuId);

			roleMenuService.insert("", entity);
		}
		
		return true;
	}
}
